package com.leetcode.easy;

import java.util.*;

public class LengthOfLongestSubstring {

    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("dvdf"));
        System.out.println(lengthOfLongestSubstring("abcabcbb"));
        System.out.println(lengthOfLongestSubstring("pwwkew"));
        System.out.println(lengthOfLongestSubstring("aab"));
    }
    public static int lengthOfLongestSubstring(String s) {

        int length = s.length();
        Set<String> set = new HashSet<>();
        HashMap<String, Integer> hashMap = new HashMap<>();
        int result = 0;
        for (int i = 0; i < length; i++) {
            String s1 = s.substring(i, i + 1);
            if (hashMap.containsKey(s1)){
                i = hashMap.get(s1);
                set.clear();
                hashMap.clear();
                set.add(s1);
            } else {
                hashMap.put(s1, i);
                set.add(s1);
            }
            result = result > set.size() ? result : set.size();
        }
        return result;
    }
}
